home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / DirectoryArchiver.java < prev    next >
Text File  |  1998-11-03  |  11KB  |  400 lines

  1. package com.symantec.itools.tools.archive;
  2.  
  3.  
  4. import java.io.BufferedInputStream;
  5. import java.io.BufferedOutputStream;
  6. import java.io.File;
  7. import java.io.FileNotFoundException;
  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.IOException;
  12. import java.util.Enumeration;
  13. import java.util.Hashtable;
  14. import java.util.Vector;
  15. import java.util.zip.ZipEntry;
  16. import java.util.zip.ZipFile;
  17. import com.symantec.itools.lang.Classpath;
  18. import com.symantec.itools.lang.ClasspathEntry;
  19. import com.symantec.itools.lang.JavaName;
  20. import com.symantec.itools.lang.NotJavaNameException;
  21. import com.symantec.itools.io.FileSystem;
  22. import com.symantec.itools.io.Directory;
  23. import com.symantec.itools.io.NotDirectoryException;
  24. import com.symantec.itools.lang.Debug;
  25.  
  26.  
  27. /**
  28.  * @author Symantec Internet Tools Division
  29.  * @version 1.0
  30.  * @since VCafe 3.0
  31.  */
  32.  
  33. public class DirectoryArchiver
  34.     extends TypeArchiver
  35. {
  36.     public DirectoryArchiver(Options options)
  37.     {
  38.         super(options);
  39.     }
  40.  
  41.     /**
  42.      * @param errorMsg TODO
  43.      * @since VCafe 3.0
  44.      */
  45.  
  46.     public boolean create(StringBuffer errorMsg)
  47.     {
  48.         String    outDir;
  49.         Classpath classpath;
  50.         Directory directory;
  51.  
  52.         outDir    = options.getOutputLocation();
  53.         classpath = new Classpath(options.getClasspath());
  54.  
  55.         try
  56.         {
  57.             directory = new Directory(outDir, true);
  58.         }
  59.         catch(NotDirectoryException ex)
  60.         {
  61.             errorMsg.append(outDir).append(" is a file not a directory");
  62.  
  63.             return (false);
  64.         }
  65.         catch(FileNotFoundException ex)
  66.         {
  67.             errorMsg.append(outDir).append(" cannot be created");
  68.  
  69.             return (false);
  70.         }
  71.         catch(IOException ex)
  72.         {
  73.             errorMsg.append("Error creating " + outDir);
  74.  
  75.             return (false);
  76.         }
  77.  
  78.         outDir = FileSystem.getCanonicalPath(outDir, true);
  79.                     
  80.         if(!(copyFiles(outDir, options.getFiles(), classpath, errorMsg)))
  81.         {        
  82.             return (false);
  83.         }
  84.  
  85.         return (copyEntries(outDir, classpath, errorMsg));
  86.     }
  87.  
  88.     /**
  89.      * @param outDir TODO
  90.      * @param files TODO
  91.      * @param classpath TODO
  92.      * @param errorMsg TODO
  93.      * @since VCafe 3.0
  94.      */
  95.  
  96.     protected boolean copyFiles(String outDir, String[] files, Classpath classpath, StringBuffer errorMsg)
  97.     {        
  98.         for(int i = 0; i < files.length; i++)
  99.         {
  100.             File file;
  101.             BufferedInputStream  inStream;
  102.             BufferedOutputStream outStream;
  103.  
  104.             file      = new File(files[i]);
  105.             inStream  = null;
  106.             outStream = null;
  107.  
  108.             try
  109.             {
  110.                 if(file.exists() && file.isFile())
  111.                 {
  112.                     File   outFile;
  113.                     File   outDirs;
  114.                     byte[] buf;
  115.                     int    len;
  116.                     String outName;
  117.  
  118.                     outName = outDir + classpath.convertFileNameToJavaName(files[i]).getFullName().replace('/', File.separatorChar);
  119.                     outFile = new File(outName);
  120.                     outDirs = new File(outFile.getParent());
  121.  
  122.                     if(verbose)
  123.                     {
  124.                         // start event
  125.                     }
  126.                     
  127.                     if(!(outDirs.exists()))
  128.                     {
  129.                         if(!(outDirs.mkdirs()))
  130.                         {
  131.                             errorMsg.append("unable to create " + outDirs);
  132.  
  133.                             return (false);
  134.                         }
  135.                     }
  136.  
  137.                     inStream  = new BufferedInputStream(new FileInputStream(file));
  138.                     outStream = new BufferedOutputStream(new FileOutputStream(outFile));
  139.                     buf       = new byte[1024];
  140.  
  141.                     while((len = inStream.read(buf, 0, 1024)) != -1)
  142.                     {
  143.                         outStream.write(buf, 0, len);
  144.                         outStream.flush();
  145.                     }
  146.  
  147.                     if(verbose)
  148.                     {
  149.                         // end event
  150.                     }
  151.                 }
  152.             }
  153.             catch(IOException ex)
  154.             {
  155.                 Debug.logException(ex);
  156.                 errorMsg.append("Error copying " + file);
  157.  
  158.                 return (false);
  159.             }
  160.             catch(NotJavaNameException ex)
  161.             {
  162.                 Debug.logException(ex);
  163.                 errorMsg.append("Invalid entry " + file);
  164.  
  165.                 return (false);
  166.             }
  167.             finally
  168.             {
  169.                 if(inStream != null)
  170.                 {
  171.                     try
  172.                     {
  173.                         inStream.close();
  174.                     }
  175.                     catch(IOException ex)
  176.                     {
  177.                         Debug.logException(ex);
  178.                     }
  179.                 }
  180.  
  181.                 if(outStream != null)
  182.                 {
  183.                     try
  184.                     {
  185.                         outStream.flush();
  186.                         outStream.close();
  187.                     }
  188.                     catch(IOException ex)
  189.                     {
  190.                         Debug.logException(ex);
  191.                     }
  192.                 }
  193.             }
  194.         }
  195.  
  196.         return (true);
  197.     }
  198.  
  199.     /**
  200.      * @param outDir TODO
  201.      * @param classpath TODO
  202.      * @param errorMsg TODO
  203.      * @since VCafe 3.0
  204.      */
  205.  
  206.     protected boolean copyEntries(String outDir, Classpath classpath, StringBuffer errorMsg)
  207.     {
  208.         String[]  entries;
  209.         Hashtable archives;
  210.         Vector    files;
  211.  
  212.         entries  = options.getEntries();
  213.         archives = new Hashtable();
  214.         files    = new Vector();
  215.  
  216.         for(int i = 0; i < entries.length; i++)
  217.         {
  218.             ClasspathEntry entry;
  219.  
  220.             entry = classpath.find(entries[i]);
  221.  
  222.             if(entry != null)
  223.             {
  224.                 if(entry.isFile())
  225.                 {
  226.                     Vector list;
  227.  
  228.                     if(!(archives.containsKey(entry)))
  229.                     {
  230.                         archives.put(entry, new Vector());
  231.                     }
  232.  
  233.                     list = (Vector)archives.get(entry);
  234.                     list.addElement(entries[i]);
  235.                 }
  236.                 else
  237.                 {
  238.                     files.addElement(FileSystem.getCanonicalPath(entry.getName(), true) + entries[i].replace('/', File.separatorChar));
  239.                 }
  240.             }
  241.             else
  242.             {
  243.                 errorMsg.append("Cannot find " + entries[i]);
  244.                 classpath.cleanup();
  245.                 
  246.                 return (false);
  247.             }
  248.         }
  249.         
  250.         classpath.cleanup();
  251.  
  252.         if(archives.size() > 0)
  253.         {
  254.             for(Enumeration e = archives.keys(); e.hasMoreElements();)
  255.             {
  256.                 ClasspathEntry key;
  257.                 Vector         list;
  258.                 String[]       array;
  259.  
  260.                 key  = (ClasspathEntry)e.nextElement();
  261.                 list = (Vector)archives.get(key);
  262.                 array = new String[list.size()];
  263.                 list.copyInto(array);
  264.  
  265.                 if(!(copyArchiveEntries(outDir, key.getName(), array, errorMsg)))
  266.                 {
  267.                     return (false);
  268.                 }
  269.             }
  270.         }
  271.  
  272.         if(files.size() > 0)
  273.         {
  274.             String[] array;
  275.  
  276.             array = new String[files.size()];
  277.             files.copyInto(array);
  278.  
  279.             if(!(copyFiles(outDir, array, classpath, errorMsg)))
  280.             {
  281.                 return (false);
  282.             }
  283.         }
  284.  
  285.         return (true);
  286.     }
  287.  
  288.     /**
  289.      * @param outDir TODO
  290.      * @param name TODO
  291.      * @param entries TODO
  292.      * @param errorMsg TODO
  293.      * @since VCafe 3.0
  294.      */
  295.  
  296.     protected boolean copyArchiveEntries(String outDir, String name, String[] entries, StringBuffer errorMsg)
  297.     {
  298.         ZipFile zip;
  299.  
  300.         try
  301.         {
  302.             zip = new ZipFile(name);
  303.         }
  304.         catch(IOException ex)
  305.         {
  306.             Debug.logException(ex);
  307.             errorMsg.append("Can't open " + name);
  308.  
  309.             return (false);
  310.         }
  311.  
  312.         for(int i = 0; i < entries.length; i++)
  313.         {
  314.             BufferedInputStream  inStream;
  315.             BufferedOutputStream outStream;
  316.  
  317.             inStream  = null;
  318.             outStream = null;
  319.  
  320.             try
  321.             {
  322.                 File     outFile;
  323.                 File     outDirs;
  324.                 byte[]   buf;
  325.                 int      len;
  326.                 String   outName;
  327.                 ZipEntry entry;
  328.                 long     remaining;
  329.  
  330.                 outName = FileSystem.getCanonicalPath(outDir, true) + entries[i].replace('/', File.separatorChar);
  331.                 outFile = new File(outName);
  332.                 outDirs = new File(outFile.getParent());
  333.  
  334.                 if(!(outDirs.exists()))
  335.                 {
  336.                     if(!(outDirs.mkdirs()))
  337.                     {
  338.                         errorMsg.append("unable to create " + outDirs);
  339.  
  340.                         return (false);
  341.                     }
  342.                 }
  343.  
  344.                 entry     = zip.getEntry(entries[i]);
  345.                 inStream  = new BufferedInputStream(zip.getInputStream(entry));
  346.                 outStream = new BufferedOutputStream(new FileOutputStream(outFile));
  347.                 buf       = new byte[1024];
  348.                 remaining = entry.getSize();
  349.  
  350.                 while((len = inStream.read(buf, 0, (int)Math.min(remaining, 1024))) != -1)
  351.                 {
  352.                     outStream.write(buf, 0, len);
  353.                     remaining -= len;
  354.                 }
  355.             }
  356.             catch(IOException ex)
  357.             {
  358.                 Debug.logException(ex);
  359.                 errorMsg.append("Error copying " + entries[i] + "to " + outDir);
  360.  
  361.                 return (false);
  362.             }
  363.             finally
  364.             {
  365.                 if(inStream != null)
  366.                 {
  367.                     try
  368.                     {
  369.                         inStream.close();
  370.                     }
  371.                     catch(IOException ex)
  372.                     {
  373.                     }
  374.                 }
  375.  
  376.                 if(outStream != null)
  377.                 {
  378.                     try
  379.                     {
  380.                         outStream.close();
  381.                     }
  382.                     catch(IOException ex)
  383.                     {
  384.                     }
  385.                 }
  386.             }
  387.         }
  388.  
  389.         return (true);
  390.     }
  391.  
  392.     /**
  393.      * @since VCafe 3.0
  394.      */
  395.  
  396.     public void cancel()
  397.     {
  398.         // TODO - implement cancel functionality
  399.     }
  400. }